package module.map.operations.points
{
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.propertys.NumberProperty;
	import drongo.model.values.CollectionValue;
	
	import flash.geom.Point;
	
	import common.operations.BaseOperation;
	
	import module.map.model.NodeTypes;
	import module.map.model.Propertys;
	
	public class AddPointOperation extends BaseOperation
	{
		private var oldState:uint=0;
		private var lastPoint:CollectionValue;
		private var currentPoint:CollectionValue;
		private var mousePoint:Point;
		private var node:CollectionValue;
		private var points:ArrayProperty;
		private var newPoint:CollectionValue;
		public function AddPointOperation()
		{
			super();
		}
		
		override public function execute():void
		{
			this.lastPoint=this.data.lastPoint;
			this.currentPoint=this.data.currentPoint;
			this.mousePoint=this.data.mousePoint;
			this.node=this.data.node;
			this.points=this.node.Get(Propertys.POINTS) as ArrayProperty;
			
			this.newPoint=NodeTypes.CreatePoint();
			this.newPoint.Update(Propertys.X,mousePoint.x);
			this.newPoint.Update(Propertys.Y,mousePoint.y);
			
			this.oldState=this.project.currentMap.Get(Propertys.STATE).GetValue();
			
			this.redo();
			this.project.currentMap.recorder.link(this);
		}
		override public function redo():void
		{
			if(currentPoint==null){
				this.points.Push(this.newPoint);
			}else{
				var index:int=this.points.GetChildIndex(currentPoint);
				if(index>=0){
					this.points.AddAt(index,this.newPoint);
				}else{
					this.points.Push(this.newPoint);
				}
			}
			this.project.currentMap.Update(Propertys.STATE,1);
		}
		override public function undo():void
		{
			this.points.Remove(this.newPoint);
			this.project.currentMap.Update(Propertys.STATE,oldState);
		}
		override public function destroy():void
		{
			this.currentPoint=null;
			this.lastPoint=null;
			this.mousePoint=null;
			this.node=null;
			this.points=null;
			this.newPoint=null;
		}
	}
}